//
//  GoodsLogisticsTableViewCell.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/18.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "GoodsLogisticsTableViewCell.h"

@interface GoodsLogisticsTableViewCell ()

@property (strong, nonatomic) UILabel *dateL;
@property (strong, nonatomic) UILabel *timeL;
@property (strong, nonatomic) UIImageView *iconIV;
@property (strong, nonatomic) UILabel *statusL;
@property (strong, nonatomic) UILabel *desL;

// 虚线
@property (strong, nonatomic) UIView *dashedTopView;

// 实线
@property (strong, nonatomic) UIView *topLineView;



@end

@implementation GoodsLogisticsTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self.contentView addSubview:self.dateL];
        [self.contentView addSubview:self.timeL];
        [self.contentView addSubview:self.iconIV];
        [self.contentView addSubview:self.statusL];
        [self.contentView addSubview:self.desL];
        
        [self.contentView addSubview:self.dashedView];
        [self.contentView addSubview:self.dashedTopView];
        [self.contentView addSubview:self.topLineView];
        [self.contentView addSubview:self.bottomLineView];
        
//        [self setupUI];
    }
    
    return self;
    
}

- (void)setDict:(NSDictionary *)dict {
    
    _dict = dict;
    
 
    if (self.type == 1) {
        
        self.topLineView.hidden = YES;
        self.dashedTopView.hidden = YES;
        self.bottomLineView.hidden = YES;
        
        self.dateL.hidden = YES;
   
    }else if (self.type == 2) {
        
        self.topLineView.hidden = YES;
        self.dashedView.hidden = YES;
        
        self.dateL.textColor = Color333333;
        self.dateL.font = Font(24);
   
    }else {
        
        self.dashedView.hidden = YES;
        self.dashedTopView.hidden = YES;
        
        self.dateL.textColor = Color999999;
        self.dateL.font = Font(20);
    }
    
    self.dateL.text = dict[@"date"];
    self.timeL.text = dict[@"time"];
    self.iconIV.image = [UIImage imageNamed:dict[@"image"]];
    
    if ([dict[@"status"] length] <= 0) {
        
        
        self.statusL.hidden = YES;
        
        __weak typeof(self) weakSelf = self;
        [self.desL mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(weakSelf.iconIV.mas_right).with.offset(jisuan(32));

            make.centerY.mas_equalTo(weakSelf.iconIV.mas_centerY);
            make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-jisuan(40));
            
        }];
   
        
    }else {
        
        self.statusL.text = dict[@"status"];
    }
    
    self.desL.text = dict[@"address"];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
//    [self.tableView setCornerRadius:8 withShadow:YES withOpacity:0.6];
    
    __weak typeof(self) weakSelf = self;
    [self.iconIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.contentView.mas_left).with.offset(jisuan(142));
        make.centerY.mas_equalTo(weakSelf.contentView.mas_centerY);
        make.width.mas_equalTo(jisuan(40));
        make.height.mas_equalTo(jisuan(40));
        
    }];
    [self.statusL mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(weakSelf.iconIV.mas_right).with.offset(jisuan(32));
        make.top.mas_equalTo(weakSelf.contentView.mas_top).with.offset(jisuan(24));
    }];
    [self.desL mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(weakSelf.iconIV.mas_right).with.offset(jisuan(32));
        make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom).with.offset(-jisuan(24));
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-jisuan(40));

    }];
   
    [self.timeL mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.mas_equalTo(weakSelf.iconIV.mas_left).with.offset(-jisuan(30));
        make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom).with.offset(-jisuan(20));

    }];
    [self.dateL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.iconIV.mas_left).with.offset(-jisuan(30));
//        make.centerY.mas_equalTo(weakSelf.statusL.mas_centerY);
        
        make.bottom.mas_equalTo(weakSelf.timeL.mas_top).with.offset(-Margin);
        
    }];

    CGFloat height = (weakSelf.height - jisuan(40)) / 2.0;
    
    NSLog(@"%.2f===%.2f===%.2f", height, weakSelf.height, weakSelf.iconIV.height);
//    [self.dashedTopView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.top.mas_equalTo(weakSelf.contentView.mas_top).with.offset(jisuan(0));
//        make.centerX.mas_equalTo(weakSelf.iconIV.mas_centerX);
//        make.width.mas_equalTo(jisuan(2));
//        make.height.mas_equalTo(height);
//
//    }];
    
    CGFloat x = jisuan(142) + jisuan(20);
    self.dashedTopView.frame = CGRectMake(x, 0, jisuan(2), height);

    [self addBorderToLayer2:self.dashedTopView];
//    [self drawDashLine:self.dashedTopView lineLength:5 lineSpacing:2 lineColor:Colorff0000];
    
//    [self.dashedView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom).with.offset(jisuan(0));
//        make.centerX.mas_equalTo(weakSelf.iconIV.mas_centerX);
//        make.width.mas_equalTo(jisuan(2));
//        make.height.mas_equalTo(height);
//
//    }];
    CGFloat y = height + jisuan(40);
    self.dashedView.frame = CGRectMake(x, y, jisuan(2), height);

    [self addBorderToLayer2:self.dashedView];
//    [self drawDashLine:self.dashedView lineLength:5 lineSpacing:2 lineColor:Colorff0000];
    
    self.topLineView.frame = CGRectMake(x, 0, jisuan(2), height);
    self.bottomLineView.frame = CGRectMake(x, y, jisuan(2), height);

//    [self.topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.top.mas_equalTo(weakSelf.contentView.mas_top).with.offset(jisuan(0));
//        make.centerX.mas_equalTo(weakSelf.iconIV.mas_centerX);
//        make.width.mas_equalTo(jisuan(2));
//        make.height.mas_equalTo(height);
//
//    }];
//    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom).with.offset(jisuan(0));
//        make.centerX.mas_equalTo(weakSelf.iconIV.mas_centerX);
//        make.width.mas_equalTo(jisuan(2));
//        make.height.mas_equalTo(height);
//
//    }];
    
}

- (void)addBorderToLayer2:(UIView *)view
{

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:view.bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(view.frame) / 2, CGRectGetHeight(view.frame)/2)];
    
    [shapeLayer setStrokeColor:Colorcccccc.CGColor];
//    [shapeLayer setStrokeColor:WhiteColor.CGColor];
    [shapeLayer setLineWidth:0.5];
    //  设置线宽，线间距
    
    [shapeLayer setLineDashPattern:@[@6,@3]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    if (CGRectGetWidth(view.frame) > CGRectGetHeight(view.frame)) {
        CGPathAddLineToPoint(path, NULL, CGRectGetWidth(view.frame),0);
    }else{
        CGPathAddLineToPoint(path, NULL, 0,CGRectGetHeight(view.frame));
    }
    [shapeLayer setPath:path];
    CGPathRelease(path);
    
    //  把绘制好的虚线添加上来
    [view.layer addSublayer:shapeLayer];
}

#pragma mark -lazy
- (UILabel *)dateL {
    
    if (!_dateL) {
        
        _dateL = [[UILabel alloc] init];
        _dateL.textAlignment = NSTextAlignmentRight;
        
    }
    
    return _dateL;
}
- (UILabel *)timeL {
    
    if (!_timeL) {
        
        _timeL = [[UILabel alloc] init];
        _timeL.textColor = Color999999;
        _timeL.font = Font(20);
        _timeL.textAlignment = NSTextAlignmentRight;
        
    }

    return _timeL;
}

- (UIImageView *)iconIV {
    
    if (!_iconIV) {
    
        _iconIV = [[UIImageView alloc] init];
        _iconIV.contentMode = UIViewContentModeScaleAspectFit;
        
    }
    
    return _iconIV;
}

- (UILabel *)statusL {
    
    if (!_statusL) {
        
        _statusL = [[UILabel alloc] init];
//        _statusL.textColor = Color999999;
        _statusL.font = Font(24);
        _statusL.textAlignment = NSTextAlignmentLeft;
        
    }
    
    return _statusL;
}
- (UILabel *)desL {
    
    if (!_desL) {
        
        _desL = [[UILabel alloc] init];
        _desL.textColor = Color999999;
        _desL.font = Font(20);
        _desL.textAlignment = NSTextAlignmentLeft;
        _desL.numberOfLines = 0;
        
    }
    
    return _desL;
}
- (UIView *)dashedView {
    
    if (!_dashedView) {
        
        _dashedView = [[UIView alloc] init];
        _dashedView.backgroundColor = WhiteColor;
        
    }
    
    return _dashedView;
}
- (UIView *)dashedTopView {
    
    if (!_dashedTopView) {
        
        _dashedTopView = [[UIView alloc] init];
        _dashedTopView.backgroundColor = WhiteColor;
        
    }
    
    return _dashedTopView;
}
- (UIView *)topLineView {
    
    if (!_topLineView) {
        
        _topLineView = [[UIView alloc] init];
        _topLineView.backgroundColor = Colorcccccc;
        
    }
    
    return _topLineView;
}
- (UIView *)bottomLineView {
    
    if (!_bottomLineView) {
        
        _bottomLineView = [[UIView alloc] init];
        _bottomLineView.backgroundColor = Colorcccccc;
        
    }
    
    return _bottomLineView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
